package cn.itsource.basic.controller;

import cn.itsource.basic.basic.BusinessException;
import cn.itsource.basic.dto.CodeDto;
import cn.itsource.basic.service.IVerifyCodeService;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            //将验证码输出为base64编码的字符串
            String base64Key = verifyCodeService.imageVerifyCode(key);
            //给前端返回这个字符串
            return AjaxResult.me().setResultObj(base64Key);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"没验证成功啊，再试试呗");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult phoneVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.phoneVerifyCode(codeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            //打印错误信息
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"没验证成功啊，再试试呗");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.binderVerifyCode(codeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            //打印错误信息
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"没验证成功啊，再试试呗");
        }
    }
}
